package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.annotation.RequriedPermission;
import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.query.SystemMenuQuery;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.AjaxResult;
import cn.wolfcode.wms.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("systemMenu")
public class SystemMenuController {
    @Autowired
    private ISystemMenuService systemMenuService;
    @RequriedPermission("菜单列表")
    @RequestMapping("query")
    public String query(Model model, @ModelAttribute("qo")SystemMenuQuery qo){
        SystemMenu parent = systemMenuService.get(qo.getParentId());
        List<Map<String, Object>> parents = new ArrayList<Map<String, Object>>();
        if (parent != null) {
            Map<String, Object> menu = new HashMap<String, Object>();
            menu.put("id", parent.getId());
            menu.put("name", parent.getName());
            parents.add(menu);
            parent = parent.getParent();
        }
//		将集合中元素颠倒
        Collections.reverse(parents);
        model.addAttribute("parents", parents);
        PageInfo<SystemMenu> result = systemMenuService.query(qo);
        model.addAttribute("result",result);
        return "systemMenu/list";
    }
    @RequriedPermission("菜单编辑")
    @RequestMapping("input")
    public String input(Model model,Long id,Long parentId){
        if (parentId == null){
            model.addAttribute("parentName","根目录");
        }else {
//            通过parentId查询菜单对象
            SystemMenu systemMenu = systemMenuService.selectByParentId(parentId);
            model.addAttribute("parentId",systemMenu.getId());
            model.addAttribute("parentName",systemMenu.getName());
        }
        if (id != null){
            SystemMenu systemMenu = systemMenuService.get(id);
            model.addAttribute("systemMenu",systemMenu);
        }
        return "systemMenu/input";
    }
    @RequriedPermission("菜单保存/更新")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(SystemMenu systemMenu,Long parentId){
        if (parentId != null){
            SystemMenu menu = new SystemMenu();
            menu.setId(parentId);
            systemMenu.setParent(menu);
        }
        if (systemMenu.getId() == null){
            systemMenuService.save(systemMenu);
        }else {
            systemMenuService.update(systemMenu);
        }
        return new AjaxResult();
    }
    @RequriedPermission("菜单删除")
    @RequestMapping("delete")
    @ResponseBody
    public AjaxResult delete(Long id){
        if (id != null){
            systemMenuService.delete(id);
        }
        return new AjaxResult();
    }
//   菜单系统的权限管理,即在左侧菜单栏显示的功能;
    @RequestMapping("selectSystemBySn")
    @ResponseBody
    public List<Map<String,Object>> selectSystemBySn(String parentSn){
//  判断是否超级管理员
        if (UserContext.getEmployee().isAdmin()){
//            通过parentSn查询菜单的
            return systemMenuService.selectByParentSn(parentSn);
        }else {
            return systemMenuService.selectByEmployeeId(parentSn, UserContext.getEmployee().getId());
        }
    }
}
